package com.beiben.experiment.frock.service.impl;

import java.util.Date;
import java.util.List;

import com.beiben.common.GlobalConstant;
import com.beiben.experiment.equipment.domain.Equipment;
import com.beiben.experiment.repair.domain.Repair;
import com.beiben.experiment.repair.mapper.RepairMapper;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.impl.SysUserServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.beiben.experiment.frock.mapper.FrockMapper;
import com.beiben.experiment.frock.domain.Frock;
import com.beiben.experiment.frock.service.IFrockService;

/**
 * 工装信息Service业务层处理
 * 
 * @author 熊国强
 * @date 2020-11-11
 */
@Service
public class FrockServiceImpl implements IFrockService 
{
    private static final Logger log = LoggerFactory.getLogger(SysUserServiceImpl.class);

    @Autowired
    private FrockMapper frockMapper;

    @Autowired
    private RepairMapper repairMapper;

    /**
     * 查询工装信息
     * 
     * @param frockId 工装信息ID
     * @return 工装信息
     */
    @Override
    public Frock selectFrockById(Long frockId)
    {
        return frockMapper.selectFrockById(frockId);
    }

    /**
     * 查询工装信息列表
     * 
     * @param frock 工装信息
     * @return 工装信息
     */
    @Override
    public List<Frock> selectFrockList(Frock frock)
    {
        return frockMapper.selectFrockList(frock);
    }

    /**
     * 新增工装信息
     * 
     * @param frock 工装信息
     * @return 结果
     */
    @Override
    public int insertFrock(Frock frock)
    {
        return frockMapper.insertFrock(frock);
    }

    /**
     * 修改工装信息
     * 
     * @param frock 工装信息
     * @return 结果
     */
    @Override
    public int updateFrock(Frock frock)
    {
        return frockMapper.updateFrock(frock);
    }

    /**
     * 批量删除工装信息
     * 
     * @param frockIds 需要删除的工装信息ID
     * @return 结果
     */
    @Override
    public int deleteFrockByIds(Long[] frockIds)
    {
        repairMapper.deleteRepairByRelIds(frockIds,GlobalConstant.EXPERIMENT_FROCK);
        return frockMapper.deleteFrockByIds(frockIds);
    }

    /**
     * 删除工装信息信息
     * 
     * @param frockId 工装信息ID
     * @return 结果
     */
    @Override
    public int deleteFrockById(Long frockId)
    {
        return frockMapper.deleteFrockById(frockId);
    }

    /**
     * 导入数据
     *
     * @param list 数据列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param
     * @return 结果
     */
    @Override
    public String importFrock(List<Frock> list, Boolean isUpdateSupport) {
        if (StringUtils.isNull(list) || list.size() == 0)
        {
            throw new CustomException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (Frock frock : list)
        {
            try
            {
                // 验证是否存在这个用户
                Frock f = frockMapper.selectFrockByCode(frock.getCode());
                if (StringUtils.isNull(f))
                {
                    this.insertFrock(frock);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、编号： " + frock.getCode() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    frock.setFrockId(f.getFrockId());
                    this.updateFrock(frock);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、编号: " + frock.getCode() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、编号: " + frock.getCode() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、编号: " + frock.getCode() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共: " + failureNum + " 条数据格式不正确，错误如下：");
            throw new CustomException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共: " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    /**
     * 修改工装信息状态
     *
     * @param FrockIds 工装信息IDS
     * @param state 使用状态
     * @return 结果
     */
    @Override
    public int updateFrockState(Long[] FrockIds, String state) {
        if("0".equals(state)){
            Repair repair = new Repair();
            repair.setRelIds(FrockIds);
            repair.setTableName(GlobalConstant.EXPERIMENT_FROCK);
            repair.setRelFinishTime(new Date());
            repair.setIsFinish("Y");
            repairMapper.updateRepairIsFinish(repair);
        }
        return frockMapper.updateFrockState(FrockIds,state);
    }
}
